package com.ymatou.iapi.reachpromotion.testcase;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymatou.iapi.marketingmanage.parameter.CreateSellerPromotionBean;
import com.ymatou.iapi.reachpromotion.parameter.GetPromotionByIdBean;
import com.ymatou.iapi.reachpromotion.parameter.PromotionPolicy;
import com.ymatou.iapi.reachpromotion.parameter.PromotionPolicys;
import com.ymatou.iapi.reachpromotion.service.GetPromotionByIdCall;
import com.ymatou.iapi.reachpromotion.service.PromotionInfoVerify;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JSellerPromotionCallService;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;
/**
*根据促销ID、买手ID查询促销详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetPromotionById{
	private static GetPromotionByIdBean getpromotionbyidBean;
	private static GetPromotionByIdCall getpromotionbyidCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据促销ID、买手ID查询促销详情");
	}

	@Before
	public void caseUp() {
		getpromotionbyidBean=new GetPromotionByIdBean();
		getpromotionbyidCall=new GetPromotionByIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getpromotionbyidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_001() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-显示商品");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			
			getpromotionbyidBean.setSellerId(createSellerPromotionBean.getSellerId());
			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
			promotionInfoVerify.verifyPromotionPolicyInDB(promotionPolicy);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionById_002() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-id空");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			
			getpromotionbyidBean.setSellerId(createSellerPromotionBean.getSellerId());
//			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			Logger.verifyEquals(null, promotionPolicy, "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionById_003() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-id不存在");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			
			getpromotionbyidBean.setSellerId(createSellerPromotionBean.getSellerId());
			getpromotionbyidBean.setPromotionId(999999);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			Logger.verifyEquals(null, promotionPolicy, "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionById_004() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-seller不传");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			
//			getpromotionbyidBean.setSellerId(createSellerPromotionBean.getSellerId());
			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			Logger.verifyEquals(null, promotionPolicy, "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionById_005() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-seller不正确");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			
			getpromotionbyidBean.setSellerId(33344);
			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			Logger.verifyEquals(null, promotionPolicy, "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionById_006() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-活动已取消");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			Logger.verifyEquals(null, promotionPolicy, "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionById_007() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-活动未开始");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			
			createSellerPromotionBean.getPromotionInfo().setPromotionBeginTime(YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING));
			createSellerPromotionBean.getPromotionInfo().setPromotionEndTime(YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING));

			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);

			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			Logger.verifyEquals(null, promotionPolicy, "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetPromotionById_008() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-新老接口对比");
		try {
			
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsSpecifyProduct",true);
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			
			getpromotionbyidBean.setSellerId(sellerId);
			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			Logger.verifyEquals(null, promotionPolicy, "验证返回Code");
			
			String url = getpromotionbyidCall.getUrl();
			url=url.replace("reachpromotion.iapi.ymatou.com", "sellerpromotion.api.ymatou.com");
		
			getpromotionbyidCall.setUrl(url);
			getpromotionbyidCall.callService();	

			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");

			PromotionPolicy promotionPolicy2 = getpromotionbyidCall.getPromotionInfo();
			Logger.verifyEquals(true, new PromotionInfoVerify().checkPromotionPolicy(promotionPolicy, promotionPolicy2), "验证新老接口返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionById_009() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-显示商品：带指定商品");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			
			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();

			PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
			promotionInfoVerify.verifyPromotionPolicyInDB(promotionPolicy);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetPromotionById_010() {
		Logger.start(true,"根据促销ID、买手ID查询促销详情-新老接口对比-stg");
		try {
			
			int sellerId = 3207973;
			int promotionId = 16983;
			
			getpromotionbyidBean.setSellerId(sellerId);
			getpromotionbyidBean.setPromotionId(promotionId);
			getpromotionbyidBean.setIsShowProducts(true);
			
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();	
			
			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");
			PromotionPolicy promotionPolicy = getpromotionbyidCall.getPromotionInfo();
			
			String url = getpromotionbyidCall.getUrl();
			url=url.replace("reachpromotion.iapi.ymatou.com", "sellerpromotion.api.ymatou.com");
		
			getpromotionbyidCall.setUrl(url);
			getpromotionbyidCall.callService();	

			Logger.verifyEquals("200", getpromotionbyidCall.getCode(), "验证返回Code");

			PromotionPolicy promotionPolicy2 = getpromotionbyidCall.getPromotionInfo();
			Logger.verifyEquals(true, new PromotionInfoVerify().checkPromotionPolicy(promotionPolicy, promotionPolicy2), "验证新老接口返回");

			AssertHelper.assertResultEqual(promotionPolicy, promotionPolicy2, "GetPromotionById");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}